﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SharpDiary.Core.Controls
{
    public partial class EditableLabel : UserControl
    {
        private bool _readOnly = false;

        private string _caption = string.Empty;

        /// <summary>
        /// 获取或设置是否只读。
        /// </summary>
        public bool ReadOnly
        {
            get { return _readOnly; }
            set { _readOnly = value; }
        }

        private string _watermarkText = "要修改，请点击此处";

        public string WatermarkText
        {
            get { return _watermarkText; }
            set
            {
                _watermarkText = value;
                if (string.IsNullOrEmpty(_caption)) { lblText.Text = value; }
            }
        }

        public string Caption
        {
            get { return _caption; }
            set
            {
                _caption = value;
                lblText.Text = value;
                txtText.Text = value;

                //Caption 为空时，显示 watermarkText 文字。
                if (string.IsNullOrEmpty(_caption))
                {
                    lblText.ForeColor = Color.FromKnownColor(KnownColor.GrayText);
                    lblText.Text = _watermarkText;
                }
                else
                {
                    lblText.ForeColor = this.ForeColor;
                }
            }
        }

        /// <summary>
        /// 切换到编辑状态。
        /// </summary>
        public void Edit()
        {
            if (ReadOnly) { return; }

            lblText.Hide();
            txtText.Show();
            txtText.Focus();
            txtText.SelectAll();

            if (string.IsNullOrEmpty(_caption)) { txtText.Text = string.Empty; }

            Application.DoEvents();
        }

        /// <summary>
        /// 更新相关控件的位置。
        /// </summary>
        protected virtual void UpdateControlsLocation()
        {
            lblText.Width = Width - 2;
            txtText.Width = Width - 2;

            lblText.Location = new Point(((Width - lblText.Width) / 2) + 1, (Height - lblText.Height) / 2);
            txtText.Location = new Point(((Width - txtText.Width) / 2) + 1, (Height - txtText.Height) / 2);
        }

        protected override void OnFontChanged(EventArgs e)
        {
            base.OnFontChanged(e);

            UpdateControlsLocation();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            UpdateControlsLocation();
        }

        /// <summary>
        /// 切换到读状态。
        /// </summary>
        private void SwitchToReadMode()
        {
            txtText.Hide();
            lblText.Show();

            Application.DoEvents();

            if (txtText.Modified)
            {
                _caption = txtText.Text;
                lblText.ForeColor = this.ForeColor;
            }

            if (string.IsNullOrEmpty(txtText.Text))
            {
                lblText.Text = _watermarkText;
                lblText.ForeColor = Color.FromKnownColor(KnownColor.GrayText);
            }
        }

        public EditableLabel()
        {
            InitializeComponent();

            DoubleBuffered = true;
            ResizeRedraw = true;

            BackColor = Color.Transparent;
        }

        private void txtText_TextChanged(object sender, EventArgs e)
        {
            lblText.Text = txtText.Text;
        }

        private void txtText_Leave(object sender, EventArgs e)
        {
            SwitchToReadMode();
        }

        private void EditableLabel_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void lblText_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void txtText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) { SwitchToReadMode(); }
        }
    }
}
